package com.sfh.cpc.note;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.sfh.cpc.R;
import com.sfh.cpc.com.entity.RspBase;
import com.sfh.cpc.com.req.ReqNoteAdd;
import com.sfh.cpc.com.ui.SelectTakeImageActivity;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.com.widget.WidgetMultiSelectPictureLayout;
import com.sfh.cpc.note.model.NoteAddModel;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.ui.dialog.DialogBuilder;

import java.util.List;

public class NoteAddActivity extends SelectTakeImageActivity<NoteAddModel> {

    public static void startUI(Activity context) {

        Intent intent = new Intent(context, NoteAddActivity.class);
        context.startActivityForResult(intent, 1001);
    }

    public static void startUI(Activity context, long massID, boolean vip) {

        Intent intent = new Intent(context, NoteAddActivity.class);
        intent.putExtra("ID", massID);
        intent.putExtra("VIP", vip);
        context.startActivityForResult(intent, 1000);
    }

    private WidgetBaseToolbar toolbar;
    private EditText etContent;
    private TextView tvSize;
    private WidgetMultiSelectPictureLayout gvImage;
    private EditText etLable;
    private RadioButton rbYES, rbNO;

    private ReqNoteAdd req = new ReqNoteAdd();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.note_add_activity);
        StatusBarUtil.setColor(this, Color.WHITE, 0);
        this.toolbar = findViewById(R.id.toolbar);
        this.toolbar.setTitle("发布");
        this.etContent = findViewById(R.id.etContent);
        this.tvSize = findViewById(R.id.tvSize);
        this.gvImage = findViewById(R.id.gvImage);
        this.etLable = findViewById(R.id.etLable);
        this.rbYES = findViewById(R.id.rbYES);
        this.rbNO = findViewById(R.id.rbNO);

        req.setMassId(getIntent().getLongExtra("ID", 0));
        gvImage.setAdapter(9, 4, new WidgetMultiSelectPictureLayout.OnItemSelectListener() {
            @Override
            public void onClickSelect(int size) {
                selectSize = size;
                hasPermissions();
            }

            @Override
            public void onClickDel(final int position, String data) {
                DialogBuilder builder = new DialogBuilder();
                builder.setTitle("提示");
                builder.setMessage("当前删除吗?");
                builder.setOkListener(new DialogBuilder.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogBuilder.DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        gvImage.onDel(position);
                    }
                });
                showDialog(builder);
            }
        });
        etContent.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int size = 100 - s.length();
                tvSize.setText(size + "/100");
            }
        });
        rbYES.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                findViewById(R.id.ly_vip).setVisibility(b ? View.VISIBLE : View.GONE);
            }
        });

        boolean vip = getIntent().getBooleanExtra("VIP", false);
        rbYES.setChecked(vip);
    }


    public void onViewCilcked(View v) {

        req.setContent(etContent.getText().toString());
        req.setLable(etLable.getText().toString());
        req.setVip(rbYES.isChecked() ? "1" : "0");
        req.setImages(gvImage.getDataList());
        EditText et_wealth = findViewById(R.id.et_wealth);
        if (!TextUtils.isEmpty(et_wealth.getText().toString())) {
            req.setWealth(Long.valueOf(et_wealth.getText().toString()));
        }
        this.getViewModel().saveNote(req);

    }

    @LiveDataMatch
    public void onAddSuccess(RspBase base) {
        if (base.success()) {
            showToast("发布成功");
            setResult(RESULT_OK);
            finish();
        } else {
            showToast("发布失败");
        }
    }

    @LiveDataMatch
    public void onSaveFile(String soure, String url) {
        gvImage.updateStatus(soure, url);
    }

    @Override
    public void onSelectImageOK(List<String> select) {
        gvImage.onAdd(select);
        for (String file : select) {
            this.getViewModel().updateFile(file);
        }
    }
}
